﻿/*-----------------------------------------------
// File: TweenNumText.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

public class TweenNumText : MonoBehaviour {

    public UILabel label;
    public float duration;
    public float delay;
    public int from;
    public long to;


    float _curTime;
    bool isPlaying = false;

    public void Init(int from, long to) {
        this.from = from;
        this.to = to;
    }

    public void ResetToBeginning() {
        _curTime = 0;
        label.text = from.ToString();
        isPlaying = false;
    }
    public void Play() {
        isPlaying = true;
    }

    public void Over() {
        label.text = to.ToString();
        isPlaying = false;
    }

	void Update () {
        if (isPlaying) {
            _curTime += Time.deltaTime;
            if (_curTime < delay)
            {
                return;
            }
            else if (_curTime >= delay && _curTime < duration + delay)
            {
                float _de = _curTime - delay;
                float value = (float)(to - from) / duration * _de;
                label.text = ((int)value).ToString();
            }
            else {
                Over();
            }

        }
	
	}
}
